package com.gubei.oss.service;

import com.gubei.oss.adapter.StorageAdapter;
import com.gubei.oss.adapter.StorageAdapterFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FileService {

    private final StorageAdapter storageAdapter;

    public FileService(
            @Qualifier("storageService")
            StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }

    public List<String> getAllBucket(){ return storageAdapter.getAllBuckets(); }

    public String getUrl(String bucket, String objectName) {
        return storageAdapter.getUrl(bucket, objectName);
    }

    public String uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        storageAdapter.uploadFile(uploadFile, bucket, objectName);
        objectName = objectName + "/" + uploadFile.getOriginalFilename();
        return storageAdapter.getUrl(bucket, objectName);
    }
}
